/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-10
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.BargainBuy;
import com.rzico.order.entity.BargainOrder;
import com.rzico.order.entity.BargainPeriod;
import com.rzico.order.service.BargainBuyService;
import com.rzico.order.service.BargainOrderService;
import com.rzico.order.service.BargainPeriodService;
import com.rzico.order.service.BargainProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 砍价查询接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-09-10
 */
@Api(description = "砍价查询接口")
@RestController
@RequestMapping("/bargainPeriod")
public class BargainPeriodController extends BaseController {

    @Autowired
    private BargainBuyService bargainBuyService;

    @Autowired
    private BargainProductService bargainProductService;

    @Autowired
    private BargainOrderService bargainOrderService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private BargainPeriodService bargainPeriodService;

    /**
     * 查询单条团购管理
     *
     * @return
    */
    @ApiOperation("查询单条团购管理")
    @GetMapping("/find")
    public CommResult<BargainBuy> find(Long orderId) {

        BargainPeriod bargainPeriod = bargainPeriodService.findByOrderId(orderId);

        BargainBuy bargainBuy = bargainBuyService.selectByPrimaryKey(bargainPeriod.getBargainBuyId());

        bargainBuy.setItemList(bargainProductService.getItemList(bargainBuy.getId()));

        Map<String,Object> data = new HashMap<>();
        data.put("bargainBuy",bargainBuy);
        data.put("bargainPeriod",bargainPeriod);
        data.put("orderItem",bargainOrderService.getItemList(bargainPeriod.getId()));
        return CommResult.success(data);

    }

    /**
     * 砍一刀
     *
     * @return
     */
    @ApiOperation("砍一刀")
    @GetMapping("/bargain")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "periodId", value = "期数ID", dataType = "String", paramType = "query"),
    })
    public CommResult<BargainOrder> bargain(Long periodId) {
        Member member = memberService.getCurrent();
        if (member==null) {
            return CommResult.error("没有登录");
        }

        bargainPeriodService.addBargain(periodId,member.getId());

        Map<String,Object> data = new HashMap<>();
        data.put("orderItem",bargainOrderService.getItemList(periodId));
        return CommResult.success(data);

    }


}
